package se.webbzon.boltzmann.game.object.waypoint;

import javax.vecmath.Point3d;

public class TargetDirection extends AbstractDirection {
	
	// The position where the target was paused
	private int pausePosition;
	
	// The mode in which the target was paused
	private int pauseMode;
	
	/*============================================================
	Constructors
	============================================================*/

	public TargetDirection(Target target, Waypoint start) {
		super(target,start);
		target.setPosition(start.getX(),start.getY(),start.getZ());
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public Target getDirectionTaker() {
		return (Target) super.getDirectionTaker();
	}
	
	@Override protected void moveTo(Point3d position) {
		Target target = getDirectionTaker();
		double dx = position.getX() - target.getX();
		double dy = position.getY() - target.getY();
		double dz = position.getZ() - target.getZ();
		double distance = target.distanceTo(position);
		double v = 3;
		double s = v/distance;
		target.setVelocity(s*dx, s*dy, s*dz);
	}

	@Override protected void onBegin() {
		Target target = getDirectionTaker();
		pausePosition = target.pos();
		pauseMode = target.isPlaying() ? 1 : target.isRewinding() ? -1 : 0;
		target.stop();
	}

	@Override protected void onEnd() {
		Target target = getDirectionTaker();
		target.seek(pausePosition);
		if (pauseMode == 1)
			target.play();
		else if (pauseMode == -1)
			target.rewind();
	}

}
